# 方式1：通过类的绑定方式
# class test:
#     __instance = None
#     def __init__(self,name,age):
#         self.name=name
#         self.age=age
#
#     @classmethod
#     def singleton(cls):
#         if not cls.__instance:
#             cls.__instance = cls("lxx",18)
#         return cls.__instance
#
# obj0 = test.singleton()
# obj1 = test.singleton()
# print(obj0)
# print(obj1)


# 方式2：通过元类来实行
# import settings
# class test2(type):
#     def __init__(self,name,base,dic):
#         self.__instance=self.__new__(self)
#         self.__init__(self.__instance,settings.PORT,settings.HOST)
#         super().__init__(name,base,dic)
#
#     def __call__(self, *args, **kwargs):
#         if args or kwargs:
#             obj = self.__new__(self)
#             self.__init__(obj,*args,**kwargs)
#             return obj
#         return self.__instance
#
# class Mysingleton(metaclass=test2):
#     def __init__(self,name,age):
#         self.name=name
#         self.age=age
#
# obj1 = Mysingleton()
# obj2 = Mysingleton()
# print(obj1)
# print(obj2)


# 方式3：通过装饰武器的方式来实行
import settings
def inner(cls):
    __instance = cls(settings.HOST,settings.PORT)
    def auth(*args,**kwargs):
        if args or kwargs:
            obj = cls(*args,**kwargs)
            return obj
        return __instance
    return auth

@inner
class Mysingleton:
    def __init__(self,name,age):
        self.name=name
        self.age=age

obj1 = Mysingleton()
obj2 = Mysingleton()
print(obj1)
print(obj2)
